package com.leetcode.题库;

import com.leetcode.common.model.ListNode;

/**
 * @author 覃国强
 * @date 2022-03-23 16:35
 */
public class 题库_0160_相交链表 {

  static
  public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {

      if (headA == null || headB == null) {
        return null;
      }
      if (headA == headB) {
        return headA;
      }

      ListNode pointA = headA;
      ListNode pointB = headB;

      while (pointA != pointB) {
        if (pointA == null) {
          pointA = headB;
        } else {
          pointA = pointA.next;
        }
        if (pointB == null) {
          pointB = headA;
        } else {
          pointB = pointB.next;
        }
      }

      return pointA;
    }
  }

}
